package com.qyhl.webtv.module_circle.circle.complain;


import com.google.gson.Gson;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.module_circle.common.CircleUrl;
import com.qyhl.webtv.module_circle.common.CircleUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by qyhl on 2018/1/24.
 */

public class CircleComplainModel implements CircleComplainContract.ComplainModel {
    private CircleComplainPresenter mPresenter;

    public CircleComplainModel(CircleComplainPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    /**
     * 举报圈子内容
     *
     * @param topicId
     * @param dstUsername
     */
    @Override
    public void doComplain(String topicId, String dstUsername, String type, String content) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;
        String userName = CommonUtils.getInstance().getUserName();

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=report" + "&username=" + userName + "&objectId=" + topicId + "&type=" + type
                        + "&content=" + content + "&dstUsername=" + dstUsername))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.complainError("网络异常，请稍后再试！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.complainSuccess("举报成功！");
                        } else {
                            mPresenter.complainError(CircleUtils.getCircleMessage(apiResult.getCode()));
                        }
                    }
                });

    }
}
